import request from '@/utils/request'

export function getSyncConfigList(params) {
  return request({
    url: '/sync/config/list',
    method: 'get',
    params
  })
}

export function getSyncConfigById(id) {
  return request({
    url: `/sync/config/${id}`,
    method: 'get'
  })
}

export function createSyncConfig(data) {
  return request({
    url: '/sync/config',
    method: 'post',
    data
  })
}

export function updateSyncConfig(data) {
  return request({
    url: '/sync/config',
    method: 'put',
    data
  })
}

export function deleteSyncConfig(id) {
  return request({
    url: `/sync/config/${id}`,
    method: 'delete'
  })
}

export function getFieldMappings(configId) {
  return request({
    url: `/sync/config/${configId}/fields`,
    method: 'get'
  })
}

export function updateFieldMappings(configId, data) {
  return request({
    url: `/sync/config/${configId}/fields`,
    method: 'put',
    data
  })
}

// 生成同步配置
export function generateSyncConfig(data, configId) {
  if (configId) {
    // 更新配置
    return request.post(`/sync/config/generate/${configId}`, data)
  } else {
    // 生成新配置
    return request.post('/sync/config/generate', data)
  }
} 

// 验证Kafka主题和消费组是否重名
export function validateKafkaConsumerGroup(topicName, consumerGroup) {
  // json 提交
  return request.post(`/sync/config/generate/kafkaTopicCheck`, JSON.stringify({
    "kafkaTopic": topicName,
    "kafkaConsumerGroup": consumerGroup
  }))
}